package ru.housewife.controllers;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import ru.common.hibernate.HibernateActionExecutor;
import ru.housewife.form.UserForm;
import ru.housewife.form.UsersTableBriefBlockForm;
import ru.housewife.form.UsersTableForm;
import ru.housewife.service.userstables.AddNewUsersTable;
import ru.housewife.service.userstables.RemoveUsersTable;
import ru.housewife.service.userstables.UpdateUsersTable;

import javax.servlet.http.HttpServletRequest;

import java.beans.PropertyEditor;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.POST;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * controller for managing users tables
 *
 * @author Alexander Troshanin
 */
@Controller
public class UsersTableController extends AuthorizedController {

    private final HibernateActionExecutor hiber;

    public UsersTableController(@Qualifier("houseWifesHibernate") HibernateActionExecutor hiber) {
        this.hiber = hiber;
    }

    @RequestMapping(value = "/userstables-list.html")
    public ModelAndView getUsersTables(HttpServletRequest request) {
        UserForm user = getUserForm(request);
        ModelAndView ret = new ModelAndView("housewifes/userstables/usersTablesCoolPage");
        List<UsersTableBriefBlockForm> usersTablesBriefs = new LinkedList<UsersTableBriefBlockForm>();
        user.setUsersTableBriefBlocks(usersTablesBriefs);
        for (UsersTableForm userTable : user.getUsersTables()) {
            usersTablesBriefs.add(userTable.getUsersTableBriefBlockForm());
        }
        ret.addObject("user", user);
        ret.addObject("usersTablesBriefs", usersTablesBriefs);
        return ret;
    }

    @RequestMapping(value = "/userstable-get.html")
    public Object getUsersTable(Long id, HttpServletRequest request) {
        UserForm user = getUserForm(request);
        UsersTableForm usersTable = user.getUsersTable(id);
        if (usersTable == null) {
            return "redirect:/userstables-list.html";
        }
//        ModelAndView ret = new ModelAndView("housewifes/userstables/usersTablePage");
        ModelAndView ret = new ModelAndView("housewifes/userstables/usersTableCoolPage");
        ret.addObject("usersTable", usersTable);
        return ret;
    }

    @RequestMapping(value = "/usertable-add.html", method = GET)
    public ModelAndView addUsersTable() {
        return new ModelAndView("housewifes/userstables/usersTablePage", "usersTable", new UsersTableForm());
    }

    @RequestMapping(value = "/usertable-save.html", method = POST)
    public Object submitSaveUsersTable(@ModelAttribute("usersTable") UsersTableForm usersTable, Errors errors, HttpServletRequest request) {
        UserForm user = getUserForm(request);
        usersTable.setUser(user);
        usersTable.validte(errors);
        if (errors.hasErrors()) {
            return new ModelAndView("housewifes/userstables/usersTablePage");
        }
        if (usersTable.getId() > 0) {
            UsersTableForm existingUsersTable = user.getUsersTable(usersTable.getId());
            if (existingUsersTable != null) {
                existingUsersTable.updateFieldsFromSrcForm(usersTable);
                hiber.execute(new UpdateUsersTable(existingUsersTable));
            }
        } else {
            hiber.execute(new AddNewUsersTable(usersTable));
        }
        return "redirect:/userstables-list.html";
    }

    @RequestMapping(value = "/userstable-remove.html")
    public Object removeUsersTable(long id, HttpServletRequest request) {
        UserForm user = getUserForm(request);
        UsersTableForm usersTable = user.getUsersTable(id);
        if (usersTable == null) {
            return "redirect:/userstables.html";
        }
        hiber.execute(new RemoveUsersTable(usersTable));
        return "redirect:/userstables.html";
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        PropertyEditor dateEditor = new CustomDateEditor(new SimpleDateFormat("dd.MM.yyyy"), true);
        binder.registerCustomEditor(Date.class, dateEditor);
    }

}
